package com.lock.method;

import java.util.Random;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @PackageName:com.lock.method
 * @ClassName:Test05
 * @Description:    boolean isHeldByCurrentThread() 判断当前线程是否持有该锁
 *                  boolean isFair() 判断是否为公平锁
 * @Author: zhangxiaoyu
 * @date 2021/3/8 13:41
 */
public class Test05 {
    static  class  Service{
        private ReentrantLock lock;
        //通过构造方法接受布尔值，确定当前锁是否公平
        public  Service (boolean isFair){
            this.lock= new ReentrantLock();
        }
        public void  serviceMethod(){
            try {
                System.out.println("是否公平锁？"+lock.isFair()+"---"+Thread.currentThread().getName()+
                        "调用lock前是否持有锁？"+lock.isHeldByCurrentThread());
                lock.lock();
                System.out.println(Thread.currentThread().getName()+"调用lock方法后是否持有锁？"+lock.isHeldByCurrentThread());
            } finally {
                //如果锁对象被当前线程持有就释放锁
                if(lock.isHeldByCurrentThread()){
                    lock.unlock();
                }
            }
        }
    }

    public static void main(String[] args){
         Runnable runnable = new Runnable() {
             @Override
             public void run() {
                 int num = new Random().nextInt();
                 new Service(num%2==0?true:false).serviceMethod();
             }
         };

         for (int i=0;i<3;i++){
             new Thread(runnable,"thread-"+i).start();
         }
    }
}
